package com.chaos.eurekaproducer.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.*;


@Configuration
public class AmqpConfig {

    final String EXCHANGE_NAME = "chaos.exchange";
    final String DEAD_EXCHANGE_NAME = "dead.exchange";
    final String DEAD_LETTER_QUEUE_NAME = "dead.letter.queue";
    final String DEAD_LETTER_ROUTING_KEY = "dead.letter.routing.key";
    final String TEST_ROUTING_KEY="chaos.routing.key";
    final String TEST_QUEUE="chaos.queue";

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    TopicExchange deadExchange() {
        return new TopicExchange(DEAD_EXCHANGE_NAME);
    }

    @Bean
    Queue deadLetterQueue() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_NAME).build();
    }
    @Bean
    Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }

    @Bean
    Binding chaosQueueBinding() {
        return BindingBuilder.bind(chaosQueue()).to(exchange()).with(TEST_ROUTING_KEY);
    }
    @Bean
    Queue chaosQueue() {
//        QueueInformation queueInfo = rabbitAdmin.getQueueInfo(TEST_QUEUE);
//        Properties queueProperties = rabbitAdmin.getQueueProperties(TEST_QUEUE);
//        String messageCount = queueProperties.getProperty("QUEUE_MESSAGE_COUNT");
//        Enumeration<Object> elements = queueProperties.elements();
//        rabbitAdmin.deleteQueue(TEST_QUEUE);
//        int unconfirmedCount = rabbitAdmin.getRabbitTemplate().getUnconfirmedCount();
//        MessageConverter messageConverter = rabbitAdmin.getRabbitTemplate().getMessageConverter();
//        Collection<MessagePostProcessor> afterReceivePostProcessors = rabbitAdmin.getRabbitTemplate().getAfterReceivePostProcessors();
//        if (!elements.hasMoreElements()){
//            return QueueBuilder.durable(TEST_QUEUE)
//                    .withArgument("x-dead-letter-exchange", DEAD_EXCHANGE_NAME)
//                    .withArgument("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY)
//                    .build();
//        }else{
//            return QueueBuilder.durable(TEST_QUEUE)
//                    .build();
//        }
        return QueueBuilder.durable(TEST_QUEUE)
//                .withArgument("x-dead-letter-exchange", DEAD_EXCHANGE_NAME)
//                .withArgument("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY)
                .build();

    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        return new MessageRecoverer() {
            @Override
            public void recover(Message message, Throwable cause) {
                //消息落库
                String message1 = cause.getCause().getMessage();
                System.out.println("消息消费失败:"+ cause.getCause());
                //发送企业微信通知
            }
        };
    }
}
